package com.hx.hospital.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hx.common.result.PageResult;
import com.hx.hospital.mapper.NotificationMapper;
import com.hx.hospital.pojo.Notification;
import com.hx.hospital.service.NotificationService;

import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;
import org.apache.dubbo.config.annotation.Service;
import java.util.List;
import java.util.Map;

@Service
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Notification> findAll() {
        return notificationMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Notification> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Notification> notifications = (Page<Notification>) notificationMapper.selectAll();
        return new PageResult<Notification>(notifications.getTotal(),notifications.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Notification> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return notificationMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Notification> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Notification> notifications = (Page<Notification>) notificationMapper.selectByExample(example);
        return new PageResult<Notification>(notifications.getTotal(),notifications.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Notification findById(Long id) {
        return notificationMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param notification
     */
    public void add(Notification notification) {
        notificationMapper.insert(notification);
    }

    /**
     * 修改
     * @param notification
     */
    public void update(Notification notification) {
        notificationMapper.updateByPrimaryKeySelective(notification);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        notificationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Notification.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // image
            if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
                criteria.andLike("image","%"+searchMap.get("image")+"%");
            }
            // desc
            if(searchMap.get("desc")!=null && !"".equals(searchMap.get("desc"))){
                criteria.andLike("desc","%"+searchMap.get("desc")+"%");
            }
            // time
            if(searchMap.get("time")!=null && !"".equals(searchMap.get("time"))){
                criteria.andLike("time","%"+searchMap.get("time")+"%");
            }


        }
        return example;
    }

}
